import { http, domain1 } from "./http";
// 轮播图
const banner = (data) =>
  http({ url: "banner/list", method: "get", data: { ...data } });
// 文章列表
const news = (data) =>
  http({ url: "cms/news/list", method: "get", data: { ...data } });
//  商品列表
const goods = (data) => http({ url: "shop/goods/list", data: { ...data } });
//  商品详情
const detail = (data) =>
  http({
    url: "shop/goods/detail?id=" + data.id,
    method: "post",
    data: { ...data },
  });
// 商品分类
const category = (data) =>
  http({ url: "shop/goods/category/all", method: "post", data: { ...data } });

// 获取code
const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};
// 获取openid
const getOpenid = (data) =>
  http({ domain: 2, url: "openid.php", method: "get", data: { ...data } });
// 获取预支付
const getPrepay = (data) =>
  http({ domain: 2, url: "jsapi.php", method: "post", data: { ...data } });
// 调起支付
const payment = (data) => {
  return new Promise((resolve, reject) => {
    wx.requestPayment({
      ...data,
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};
// 登录接口
const getLogin = (data) =>
  http({
    url: "user/m/login?deviceId=007&deviceName=monkey",
    method: "post",
    data: { ...data },
    header: { "Content-Type": "application/x-www-form-urlencoded" },
  });

// 订单创建
const order = (data) =>
  http({
    url: "order/create",
    method: "post",
    data: { ...data },
    header: { "Content-Type": "application/x-www-form-urlencoded" },
  });

// 收货地址列表
const address = (data) =>
  http({
    url: "user/shipping-address/list",
    method: "post",
    data: { ...data },
    header: { "Content-Type": "application/x-www-form-urlencoded" },
  });

//获取验证码地址
const getImageUrl = ({ key }) => {
  return `${domain1}verification/pic/get?key=${key}`;
};

//获取短信验证码
const sms = ({ mobile, picCode, key }) => {
  return http({
    url: "verification/sms/get",
    method: "POST",
    header: {
      "content-type": "application/x-www-form-urlencoded",
    },
    data: {
      mobile,
      key,
      picCode,
    },
  });
};

//请求接口，注册用户
const doRegist = ({ mobile, pwd, code, nick }) => {
  return http({
    url: "user/m/register",
    header: {
      "content-type": "application/x-www-form-urlencoded",
    },
    method: "POST",
    data: {
      mobile,
      pwd,
      code,
      nick,
    },
  });
};

//提交订单
const orderCreate = ({ token, goodsJsonStr }) => {
  return http({
    url: "order/create",
    data: { token, goodsJsonStr },
    method: "POST",
    header: {
      "content-type": "application/x-www-form-urlencoded",
    },
  });
};
export {
  banner,
  news,
  goods,
  detail,
  category,
  login,
  getOpenid,
  getPrepay,
  payment,
  getLogin,
  order,
  address,
  getImageUrl,
  sms,
  doRegist,
  orderCreate,
};
